using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

using TTDotNet.Engine.ThreeDEngine;

namespace TTDotNet.Engine.GUI
{
    public class CheckBox : Control
    {
        private GameTexture _bgChecked;
        private GameTexture _currentBg;

        public CheckBox(RectangleF screenRect, string text, float fontSize, Color textColor, string fontName, bool isChecked, GameTexture bgUnchecked, GameTexture bgChecked)
        {
            _data = isChecked;
            Checked = isChecked;

            _size = screenRect;
            _size.Width = bgUnchecked.Texture.GetLevelDescription(0).Width;
            _size.Height = bgUnchecked.Texture.GetLevelDescription(0).Height;
            _text = text;
            _fontSize = fontSize;
            _fontColor = textColor;
            textureUp = bgUnchecked;
            _bgChecked = bgChecked;
            _currentBg = bgUnchecked;
            _position = new PointF(screenRect.X, screenRect.Y);

            _textRect = screenRect;

            System.Drawing.Font systemFont = new System.Drawing.Font(fontName, fontSize);
            _font = new Microsoft.DirectX.Direct3D.Font(Game.device, systemFont);

            SetSize(screenRect.Width, screenRect.Height);
        }

        protected override void OnMouseRelease(Point cursor)
        {
            if (Contains(cursor))
            {
                _data = !(bool)_data;
                if ((bool)_data == true)
                {
                    _currentBg = _bgChecked;
                }
                else
                {
                    _currentBg = textureUp;
                }
            }
        }

        public override void Render(PointF location)
        {
            if (Checked) { _currentBg = _bgChecked; }
            else { _currentBg = textureUp; }

            Game.guiSprite.Transform = spriteTransform;
            Game.guiSprite.Draw(_currentBg.Texture, Rectangle.Empty, Vector3.Empty, Vector3.Add(_renderPosition, new Vector3(location.X / WidthScale, location.Y / HeightScale, 0)), Color.White);

            if (!_text.Equals(string.Empty))
            {
                Game.guiSprite.Transform = Matrix.Identity;
                _font.DrawText(Game.guiSprite, _text, new Rectangle((int)(_textRect.X + location.X), (int)(_textRect.Y + location.Y), (int)_textRect.Width, (int)_textRect.Height), textFormat, _fontColor);
                Game.guiSprite.Transform = spriteTransform;
            }
        }

        public virtual bool Checked
        {
            get { return (bool)_data; }
            set
            {
                if ((bool)_data == false && value == true)
                {
                    _currentBg = _bgChecked;
                }
                else if ((bool)_data == true && value == false)
                {
                    _currentBg = textureUp;
                }
                _data = value;
            }
        }

    }
}
